package com.xiaozhi.libraryfunction.controller;

import com.xiaozhi.common.annotation.Log;
import com.xiaozhi.common.constant.Constants;
import com.xiaozhi.common.constant.UserConstants;
import com.xiaozhi.common.core.controller.BaseController;
import com.xiaozhi.common.core.domain.AjaxResult;
import com.xiaozhi.common.core.domain.entity.SysRole;
import com.xiaozhi.common.core.domain.entity.SysUser;
import com.xiaozhi.common.core.page.TableDataInfo;
import com.xiaozhi.common.enums.BusinessType;
import com.xiaozhi.common.exception.job.TaskException;
import com.xiaozhi.common.utils.StringUtils;
import com.xiaozhi.common.utils.poi.ExcelUtil;
import com.xiaozhi.libraryfunction.domain.LibraryType;
import com.xiaozhi.libraryfunction.service.LibraryTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 类型信息操作处理
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/admin/library/type")
public class LibraryTypeController extends BaseController
{



    private String prefix = "library/type";

    @Autowired
    private LibraryTypeService libraryTypeService;

    @RequiresPermissions("library:type:view")
    @GetMapping()
    public String librarytype()
    {
        return prefix + "/type";
    }

    @RequiresPermissions("library:type:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LibraryType type)
    {
        startPage();
        List<LibraryType> list = libraryTypeService.selectLibraryTypeList(type);
        return getDataTable(list);
    }
    @Log(title = "类型管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("library:type:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LibraryType type)
    {
        List<LibraryType> list = libraryTypeService.selectLibraryTypeList(type);
        ExcelUtil<LibraryType> util = new ExcelUtil<LibraryType>(LibraryType.class);
        return util.exportExcel(list, "图书类型");
    }

    /**
     * 新增类型
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存类型
     */
    @RequiresPermissions("library:type:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addtypeSave(@Validated LibraryType type)
    {
        if (UserConstants.LibraryType_NAME_NOT_UNIQUE.equals(libraryTypeService.checkLibraryTypeNameUnique(type)))
        {
            return error("新增类型'" + type.getTyname() + "'失败，类型名称已存在");
        }
        return toAjax(libraryTypeService.insertLibraryType(type));

    }

    /**
     * 修改类型
     */
    @RequiresPermissions("library:type:edit")
    @GetMapping("/edit/{typeId}")
    public String typeedit(@PathVariable("typeId") Long typeId, ModelMap mmap)
    {
        mmap.put("librarytype", libraryTypeService.selectLibraryTypeById(typeId));
        return prefix + "/edit";
    }

    /**
     * 修改保存类型
     */
    @RequiresPermissions("library:type:edit")
    @Log(title = "类型管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated LibraryType type)
    {
        if (UserConstants.LibraryType_NAME_NOT_UNIQUE.equals(libraryTypeService.checkLibraryTypeNameUnique(type)))
        {
            return error("修改类型'" + type.getTyname() + "'失败，类型名称已存在");
        }
        return toAjax(libraryTypeService.updateLibraryType(type));
    }

    @RequiresPermissions("library:type:remove")
    @Log(title = "类型管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(libraryTypeService.deleteLibraryTypeByIds(ids));
    }
}
